<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/
/** 
 * Change a post's tag
 * 
 * @package Xaraya eXtensible Management System
 * @copyright (C) 2005 by the Xaraya Development Team.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.org
 *
*/

/**
 * @author John Cox
 * @author jojodee
 * add new forum topic
 */

function conversations_user_change_tag()
{

    if (!xarVarFetch('change_tag', 'str:1', $tag, '', XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('tagid', 'int:0:', $tagid, '', XARVAR_DONT_SET)) return;
    if (!xarVarFetch('postid', 'int:0:', $postid, '', XARVAR_DONT_SET)) return;
    if (!xarVarFetch('uid', 'int:0:', $uid, '', XARVAR_DONT_SET)) return;
    
    // This should be relatively simple.  The only issue will be the returning back to
    // the page with the AJAX call.
    $user = xarUserGetVar('uid');
    // We probably need some security sanity here
    if (!xarSecurityCheck('Modcon', 0)) {
        // No Privs, Hows about this is my comment?
        // FIXME: we have not checked whether this is a comment! Is 'edit' phase really supported?
        if ($user != $uid) {
            // Nope?  Lets return
            // TODO: needs to be templated.
            $message = xarML('You do not have access to modify this reply');
            return $message;
        }
    } else {
        if (!xarSecurityCheck('Postcon', 1)) return;
    }
    
    // Set up some vars
    $count = 1;
    $status = 1;
    $oldtagid = $tagid;
    
    // This should be relatively simple.  We should just have to split the tag, and then just update
    // the look-up table,  Finally we just need to delete the post fromt the original tagid in the look-up 

    // Get the tag(s)
    $tags = explode(" ", $tag);
    
    foreach ($tags as $new){
        $taginfo = xarModAPIFunc('conversations', 'user', 'gettags', array(
                                                  'tag'   => $new));
        
                                                  
        // return var_export($taginfo);
        
        // Did the function return false?  OK, then this is a new tag
        if (empty($taginfo)){
            $new = strtolower($new);
            $newtagid = xarModAPIFunc('conversations', 'user', 'createtag', array(
                                                'tag'   => $new,
                                                'status'=> $status,
                                                'uid'   => $user,
                                                'count' => $count));
            // Tag is added, now let's associate this post to it.
            //return var_export($postid);
            xarModAPIFunc('conversations', 'user', 'createlook', array(
                                        'tagid'     => $newtagid,
                                        'postid'    => $postid)); 
                           
        } else {
              
            // If there was something in taginfo, then we know that this is a merge situation
            // So lets merge 'em
            // What about multiple merges???  We really need to loop through the results
            foreach ($taginfo as $merge){
                //return var_export($merge['id']);
                xarModAPIFunc('conversations', 'user', 'createlook', array(
                                        'tagid'     => $merge['id'],
                                        'postid'    => $postid));
            }
        }    
        // that should do it with this...
    }
        
    //return 'done';
    // Regardless of what happened, we need to delete the original tag...
    
    if (!xarModAPIFunc('conversations', 'user', 'deletelook', array(
                                                'tagid' => $oldtagid,
                                                'postid' => $postid))) return;
    
    // We don't need to play with the tag, so moving on...
    // We should just have to send it to the modify function...
    
    // Just need to return the post really...
    return xarResponseRedirect(xarModURL('conversations', 'user', 'main'));
}

?>